﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class usercontrol_Products : System.Web.UI.UserControl
{
	public static string id;
    Product product = new Product();
    DaiCuongMobile.SelectedProductDataTable selectedProductTable = new DaiCuongMobile.SelectedProductDataTable(); //san pham da chon mua.
    DaiCuongMobile.SelectedProductRow selectedProductRow;  

    protected void Page_Load(object sender, EventArgs e)
    {
        DaiCuongMobile.tbCategoriesDataTable dtCategory = new DaiCuongMobile.tbCategoriesDataTable();
        DaiCuongMobileTableAdapters.tbCategoriesTableAdapter adtCategory = new DaiCuongMobileTableAdapters.tbCategoriesTableAdapter();
        if (!string.IsNullOrEmpty(Request.QueryString["cid"]))
        {
            int CateID = Convert.ToInt32(Request.QueryString["cid"]);
            dtCategory = adtCategory.GetCategoryByCateID(CateID);
            lblTitleGrid.Text = "SẢN PHẨM ĐÃ CHỌN >> " + dtCategory.Rows[0]["CategoryName"].ToString();
        }        
        if ((DaiCuongMobile.SelectedProductDataTable)Session["selectedProduct"] != null)
        {
            selectedProductTable = (DaiCuongMobile.SelectedProductDataTable)Session["selectedProduct"];
        } 
    }
    protected void DLProducts_ItemCommand(object source, DataListCommandEventArgs e)
    {
        if (e.CommandName == "Select")
        {
            int index = 0;
            if (selectedProductTable.Rows.Count > 0)
            {
                index = selectedProductTable.Rows.Count;
                index++;
            }
            else
            {
                //index = Convert.ToInt32(e.CommandArgument.ToString());
                index++;
            }
            product.ProductID = e.CommandArgument.ToString();
            product.ProductName = ((HyperLink)e.Item.FindControl("hplTenSanPham")).Text;
            Label price=(Label)e.Item.FindControl("lblGia");
            product.SalePrice = Convert.ToDecimal(price.Text.Substring(0,price.Text.Length-3));
            product.Quantity = 1;
            product.Discount = 0;

            // Them vao gio hang
            try
            {
                selectedProductRow = selectedProductTable.NewSelectedProductRow();
                selectedProductRow["Index"] = index;
                selectedProductRow["ProductID"] = product.ProductID;
                selectedProductRow["ProductName"] = product.ProductName;
                selectedProductRow["SalePrice"] = product.SalePrice;
                selectedProductRow["Quantity"] = product.Quantity;
                selectedProductRow["Discount"] = product.Discount;
                selectedProductRow["Amount"] = product.Amount;

                // kiem tra da co trong gio hang san pham nay chua
                /*BanHangOnline.SelectedProductDataTable tbSelectedProducts=selectedProductTable;
                BanHangOnline.SelectedProductRow SelectedProduct;
                for(int i=0;i<=selectedProductTable.Rows.Count-1;i++)
                {
                    if (product.ProductID == selectedProductRow.ProductID)
                    {
                    
                    }
 
                }*/

                selectedProductTable.AddSelectedProductRow(selectedProductRow);
                Session["selectedProduct"] = selectedProductTable;
            }
            catch (Exception)
            {

            }
        }    
    }
    protected void DLProducts_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        //Image img = (Image)e.Item.FindControl("imgProduct");
        //img.Attributes.Add("OnMouseOver","Tip('<%#"+ Eval('"'ProductNam'"') +" %>')");
    }
}
